package main

import (
	"fmt"
	di "go-design-pattern/02-factory/4-di"
)

// A 依赖关系 A -> B -> C
type A struct {
	B *B
}

func NewA(b *B) *A {
	return &A{
		B: b,
	}

}

type B struct {
	C *C
}

func NewB(c *C) *B {
	return &B{C: c}

}

type C struct {
	Num int
}

func NewC() *C {
	return &C{Num: 1}
}

func main() {

	container := di.New()

	err := container.Provide(NewA)
	if err != nil {
		panic(err)
	}

	err = container.Provide(NewB)
	if err != nil {
		panic(err)
	}

	err = container.Provide(NewC)
	if err != nil {
		panic(err)
	}

	err = container.Invoke(func(a *A) {
		fmt.Printf("%+v : %d ", a, a.B.C.Num)
	})

	if err != nil {
		panic(err)
	}
}
